#!/bin/bash

# 设置颜色
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

echo -e "${BLUE}"
echo "========================================"
echo "    🎯 To-Do List 待办事项应用"
echo "========================================"
echo -e "${NC}"

echo "正在启动本地服务器..."
echo

# 检查 Python 是否安装
if ! command -v python3 &> /dev/null && ! command -v python &> /dev/null; then
    echo -e "${RED}❌ 错误: 未找到 Python${NC}"
    echo "请先安装 Python 3.x 版本"
    echo "Ubuntu/Debian: sudo apt install python3"
    echo "CentOS/RHEL: sudo yum install python3"
    echo "macOS: brew install python3"
    echo
    exit 1
fi

# 选择 Python 命令
if command -v python3 &> /dev/null; then
    PYTHON_CMD="python3"
else
    PYTHON_CMD="python"
fi

# 获取本机IP地址
if [[ "$OSTYPE" == "darwin"* ]]; then
    # macOS
    IP=$(ifconfig | grep "inet " | grep -v 127.0.0.1 | awk '{print $2}' | head -1)
else
    # Linux
    IP=$(hostname -I | awk '{print $1}')
fi

echo -e "${GREEN}✅ Python 已安装${NC}"
echo -e "${GREEN}🚀 启动服务器中...${NC}"
echo

echo -e "${YELLOW}📱 访问地址:${NC}"
echo "   本地访问: http://localhost:8000"
echo "   本地访问: http://127.0.0.1:8000"
if [ ! -z "$IP" ]; then
    echo "   局域网访问: http://$IP:8000"
fi
echo

echo -e "${YELLOW}💡 提示:${NC}"
echo "   - 按 Ctrl+C 停止服务器"
echo "   - 在浏览器中打开上述任一地址即可使用"
echo "   - 手机可通过局域网地址访问"
echo

echo "========================================"
echo

# 启动 Python HTTP 服务器
$PYTHON_CMD -m http.server 8000

echo
echo "服务器已停止"